<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable width="600">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="设备类型" prop="deviceTypeId">
						<el-select v-model="form.deviceTypeId" placeholder="请选择设备类型" @change="getModelList">
							<el-option v-for="item in deviceTypeList" :key="item.id" :value="item.id" :label="item.typeName" :disabled="item.state === 20" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="设备型号" prop="deviceModelId">
						<el-select v-model="form.deviceModelId" placeholder="请选择设备型号">
							<el-option
								v-for="item in deviceModelList"
								:key="item.id"
								:value="item.id"
								:label="item.deviceModelName"
								:disabled="item.state === 20"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="IMEI" prop="imei">
						<el-input v-model="form.imei" placeholder="请输入IMEI" maxlength="100" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BasicDeviceDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/platform/basicDevice';
import { fetchListAll as fetchAllModelList } from '/@/api/platform/basicDeviceModel';
import { fetchListAll as fetchAllTypeList } from '/@/api/platform/basicDeviceType';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const deviceTypeList: any = ref([]);
const deviceModelList: any = ref([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	// qrcode: '',
	imei: '',
	// imeiAlias: '',
	deviceTypeId: null,
	deviceTypeName: null,
	deviceModelId: null,
	deviceModelName: '',
	// producer: '',
	// deviceBatchNumber: '',
	// imsi: '',
	// deptId: '',
	// packingNumber: '',
	// iccid: '',
	// deviceState: 10,
	useState: 10,
	bindState: 10,
	stocks: 10,
	// installLocationId: '',
	// model: '',
	// signals: '',
	// sort: '',
	// state: '',
	// remarks: '',
	// alarmLogId: '',
	// deviceFaultId: '',
});

// 定义校验规则
const dataRules = ref({
	imei: [
		{ required: true, message: 'IMEI不能为空', trigger: 'blur' },
		{
			validator: rule.checkSpaceInline,
			trigger: 'blur',
		},
	],
	deviceTypeId: [{ required: true, message: '设备类型不能为空', trigger: 'change' }],
	deviceModelId: [{ required: true, message: '设备型号不能为空', trigger: 'change' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
	getDeviceTypes();
	// 获取basicDevice信息
	if (id) {
		form.id = id;
		getbasicDeviceData(id);
	}
};
// 获取所有设备类型
const getDeviceTypes = () => {
	fetchAllTypeList({ state: 10 }).then((res) => {
		deviceTypeList.value = res.data;
	});
};
// 查询设备型号
const getModelList = (deviceTypeId: string) => {
	form.deviceModelId = null;
	fetchAllModelList({ deviceTypeId, state: 10 }).then((res) => {
		deviceModelList.value = res.data;
	});
};
// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		const deviceTypeItem = deviceTypeList.value.find((d) => form.deviceTypeId === d.id);
		deviceTypeItem && (form.deviceTypeName = deviceTypeItem.typeName);
		const modelTypeItem = deviceModelList.value.find((d) => form.deviceModelId === d.id);
		modelTypeItem && (form.deviceModelName = modelTypeItem.deviceModelName);
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbasicDeviceData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
